package mware_lib.communication;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private ServerSocket MySvrSocket;
	private Socket sock;

	public Server(int listenPort) throws IOException {
		MySvrSocket = new ServerSocket(listenPort);
		this.sock = null;
	}

	
	public Connection getConnection() throws IOException {
		return new Connection(MySvrSocket.accept());
	}

	public void shutdown() throws IOException {
		this.disconnect();
		MySvrSocket.close();
	}
	//Wait for a connection (blocking)
	public void accept() throws IOException {
		this.sock = MySvrSocket.accept();
	}

	public void disconnect() throws IOException {
		if (this.sock != null) {
			this.sock.close();
		}
		this.sock = null;
	}

	public ObjectStream getObjectStream() throws IOException {
		return new ObjectStream(this.sock);
	}

	public String getServerHost() {
		return MySvrSocket.getInetAddress().getHostName();
	}
}
